package fr.alma.trajectory.calculating;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import fr.alma.trajectory.commons.ASpaceObject;
import fr.alma.trajectory.commons.SpaceObject;
import fr.alma.trajectory.commons.SpacePoint;
import fr.alma.trajectory.commons.Square;
import fr.alma.trajectory.commons.Triangle;
import fr.alma.trajectory.commons.Rectangle;;

public class Calculator implements ICalculator {
	
	@Override
	public Collection<SpaceObject> calculate(SpaceObject first
											,Collection<SpacePoint> trajectory){
		
		List<SpaceObject> objects = new ArrayList<SpaceObject>();
		objects.add(first);
		
		//parcours de tous les points de la trajectoire
		for(SpacePoint p : trajectory){
			SpaceObject translated = nextObject(first, p);
			objects.add(translated);
			first = translated;
		}
		return objects;
	}
	
	private SpaceObject nextObject(SpaceObject object, SpacePoint reference){
		SpaceObject next = null;
		if(object.isTriangle()){
			next = new Triangle(new SpacePoint(reference.x, reference.y, reference.z)
							,new SpacePoint(reference.x+ASpaceObject.TRIANGLE, reference.y, reference.z)
							,new SpacePoint(reference.x+ASpaceObject.TRIANGLE/2, reference.y+ASpaceObject.TRIANGLE, reference.z)
			);
		}
		else if(object.isRectangle()){
			next = new Rectangle(new SpacePoint(reference.x, reference.y, reference.z)
							,new SpacePoint(reference.x+ASpaceObject.RECTANGLE, reference.y, reference.z)
							,new SpacePoint(reference.x+ASpaceObject.RECTANGLE, reference.y+ASpaceObject.RECTANGLE/2, reference.z)
							,new SpacePoint(reference.x, reference.y+ASpaceObject.RECTANGLE/2, reference.z)
			);
		}
		else if(object.isLosange()){
			next = new Rectangle(new SpacePoint(reference.x, reference.y, reference.z)
							,new SpacePoint(reference.x+ASpaceObject.LOSANGE, reference.y, reference.z)
							,new SpacePoint(reference.x+ASpaceObject.LOSANGE, reference.y+ASpaceObject.LOSANGE/2, reference.z)
							,new SpacePoint(reference.x, reference.y+ASpaceObject.LOSANGE/2, reference.z)
			);
		}
		else {
			next = new Square(new SpacePoint(reference.x, reference.y, reference.z)
							,new SpacePoint(reference.x+ASpaceObject.SQUARE, reference.y, reference.z)
							,new SpacePoint(reference.x+ASpaceObject.SQUARE, reference.y+ASpaceObject.SQUARE, reference.z)
							,new SpacePoint(reference.x, reference.y+ASpaceObject.SQUARE, reference.z)
			);
		}
		return next;
	}
	
}
